<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2024/2/20
 * Time: 14:20
 */

namespace app\api\controller;

use app\api\model\EmailCode;
use app\api\model\UUser;

class Apilogin extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 发送邮箱验证码
     * email   邮箱
     * type    1.注册  2.找回密码
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function sendCode(){
        $email = input('email');
        $type = input('type') ? input('type') : 0; // 1.注册  2.找回密码

        if(!$email) return json(['status' => 0, 'msg' => $this->lanMsg->requireEmail[$this->lanType]]);

        $hasUser = UUser::findData(['user_email' => $email]);

        if($type == 1){ // 邮箱已注册
            if($hasUser) return json(['status' => 0, 'msg' => $this->lanMsg->emailAlreadyRegister[$this->lanType]]);
        }elseif ($type == 2){ // 邮箱尚未注册
            if(!$hasUser) return json(['status' => 0, 'msg' => $this->lanMsg->emailYetRegister[$this->lanType]]);
        }

//        $code = GetRandCode();//6位数验证码
//        $result = $this->sendEmail($email, $code);
        $code = 111111;//6位数验证码
        $result = ['code' => 200];

        if($result['code'] == 200){
            $hasEmail = EmailCode::get(['email' => $email]);
            if($hasEmail){
                $update = [
                    'code' => $code,
                    'status' => 0,
                    'time' => time(),
                    'date_time' => initTime()
                ];
                $res = EmailCode::update($update, ['id' => $hasEmail['id']]);
            }else{
                $create = [
                    'email' => $email,
                    'code' => $code,
                    'time' => time(),
                    'date_time' => initTime()
                ];
                $res = EmailCode::create($create);
            }

            if($res) {
                return json(['status' => 200, 'msg' => $code]);
            }else{
                return json(['status' => 0, 'msg' => $this->lanMsg->systemErr[$this->lanType]]);
            }
        }else{
            return json(['status' => 0, 'msg' => $result['msg']]);
        }

    }

    /**
     * 邮箱注册
     * email     邮箱
     * code      验证码
     * pwd       密码
     * verifyPwd 确认密码
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function emailRegister(){
        $email = input('email');
        $code = input('code');
        $pwd = input('pwd');
        $verifyPwd = input('verifyPwd');

        if(!$email) return json(['status' => 0, 'msg' => $this->lanMsg->requireEmail[$this->lanType]]);
        if(!$code) return json(['status' => 0, 'msg' => $this->lanMsg->requireCode[$this->lanType]]);
        if(!$pwd) return json(['status' => 0, 'msg' => $this->lanMsg->requirePassword[$this->lanType]]);
        if(!$verifyPwd) return json(['status' => 0, 'msg' => $this->lanMsg->verifyPassword[$this->lanType]]);
        if($pwd != $verifyPwd) return json(['status' => 0, 'msg' => $this->lanMsg->passwordNotSame[$this->lanType]]);

        // 重复注册
        $hasRegister = UUser::get(['user_email' => $email]);
        if($hasRegister) return json(['status' => 0, 'msg' => $this->lanMsg->emailAlreadyRegister[$this->lanType]]);

        // 验证码
        $yzm = EmailCode::get(['email' => $email]);
        if(!$yzm) return json(['status' => 0, 'msg' => $this->lanMsg->sendCodeFirst[$this->lanType]]);
        if($yzm['status'] == 1) return json(['status' => 0, 'msg' => $this->lanMsg->codeAlreadyUse[$this->lanType]]);
        if($yzm['code'] != $code) return json(['status' => 0, 'msg' => $this->lanMsg->codeErr[$this->lanType]]);
        $now = time();
        if($now - $yzm['time'] > 300) return json(['status' => 0, 'msg' => $this->lanMsg->yzmLose[$this->lanType]]);

        $create = [
            'user_email'    => $email,
            'user_password' => md5($pwd),
            'user_time'     => initTime()
        ];

        $user_id = UUser::insertDataGetID($create);
        if($user_id){
            // 注册的用户信息
            $user = UUser::get($user_id);
            // 验证码使用状态
            EmailCode::update(['status' => 1], ['id' => $yzm['id']]);

            return json(['status' => 200, 'msg' => $this->lanMsg->registerSuccess[$this->lanType], 'user' => $user]);
        }else{
            return json(['status' => 0, 'msg' => $this->lanMsg->systemErr[$this->lanType]]);
        }
    }

    /**
     * 邮箱密码登录
     * email     邮箱
     * pwd       密码
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function login(){
        $email = input('email');
        $pwd = input('pwd');

        if(!$email) return json(['status' => 0, 'msg' => $this->lanMsg->requireEmail[$this->lanType]]);
        if(!$pwd) return json(['status' => 0, 'msg' => $this->lanMsg->requirePassword[$this->lanType]]);

        $hasUser = UUser::get(['user_email' => $email]);
        // 邮箱未注册
        if(!$hasUser) return json(['status' => 0, 'msg' => $this->lanMsg->emailYetRegister[$this->lanType]]);
        if($hasUser['user_type'] == 2) return json(['status' => 0, 'msg' => $this->lanMsg->canNotLogin[$this->lanType]]);
        // 用户状态
        if($hasUser['user_status'] == 4 || $hasUser['user_status'] == 5){
            return json(['status' => 0, 'msg' => $this->lanMsg->userStatusErr[$this->lanType]]);
        }
        // 密码错误
        $password = md5($pwd);
        if($password != $hasUser['user_password']) return json(['status' => 0, 'msg' => $this->lanMsg->passwordErr[$this->lanType]]);

        $res = UUser::update(['user_last_login_time' => initTime()], ['user_id' => $hasUser['user_id']]);
        if($res){
            return json(['status' => 200, 'msg' => $hasUser]);
        }else{
            return json(['status' => 0, 'msg' => $this->lanMsg->systemErr[$this->lanType]]);
        }
    }

    /**
     * 忘记密码
     * email     邮箱
     * code      验证码
     * pwd       密码
     * verifyPwd 确认密码
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function forgetPassword(){
        $email = input('email');
        $code = input('code');
        $pwd = input('pwd');
        $verifyPwd = input('verifyPwd');

        if(!$email) return json(['status' => 0, 'msg' => $this->lanMsg->requireEmail[$this->lanType]]);
        if(!$code) return json(['status' => 0, 'msg' => $this->lanMsg->requireCode[$this->lanType]]);
        if(!$pwd) return json(['status' => 0, 'msg' => $this->lanMsg->requirePassword[$this->lanType]]);
        if(!$verifyPwd) return json(['status' => 0, 'msg' => $this->lanMsg->verifyPassword[$this->lanType]]);
        if($pwd != $verifyPwd) return json(['status' => 0, 'msg' => $this->lanMsg->passwordNotSame[$this->lanType]]);

        $hasRegister = UUser::get(['user_email' => $email]);
        if(!$hasRegister) return json(['status' => 0, 'msg' => $this->lanMsg->emailYetRegister[$this->lanType]]);

        // 验证码
        $yzm = EmailCode::get(['email' => $email]);
        if(!$yzm) return json(['status' => 0, 'msg' => $this->lanMsg->sendCodeFirst[$this->lanType]]);
        if($yzm['status'] == 1) return json(['status' => 0, 'msg' => $this->lanMsg->codeAlreadyUse[$this->lanType]]);
        if($yzm['code'] != $code) return json(['status' => 0, 'msg' => $this->lanMsg->codeErr[$this->lanType]]);
        $now = time();
        if($now - $yzm['time'] > 300) return json(['status' => 0, 'msg' => $this->lanMsg->yzmLose[$this->lanType]]);

        $update = [
            'user_password' => md5($pwd),
            'user_update_time' => initTime()
        ];

        $res = UUser::update($update, ['user_id' => $hasRegister['user_id']]);

        if($res){
            // 验证码使用状态
            EmailCode::update(['status' => 1], ['id' => $yzm['id']]);
            return json(['status' => 200, 'msg' => $this->lanMsg->doSuccess[$this->lanType]]);
        }else{
            return json(['status' => 0, 'msg' => $this->lanMsg->systemErr[$this->lanType]]);
        }
    }
}